package com.xjrsoft.module.erpModel.caseErpSupplier.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.xjrsoft.common.constant.GlobalConstant;
import com.xjrsoft.module.erpModel.caseErpLog.entity.CaseErpLog;
import com.xjrsoft.module.erpModel.caseErpLog.service.ICaseErpLogService;
import com.xjrsoft.module.erpModel.caseErpSupplier.dto.AddCaseErpSupplierFormalDto;
import com.xjrsoft.module.erpModel.caseErpSupplier.entity.CaseErpSupplier;
import com.xjrsoft.module.erpModel.caseErpSupplier.entity.CaseErpSupplierFormal;
import com.xjrsoft.module.erpModel.caseErpSupplier.mapper.CaseErpSupplierFormalMapper;
import com.xjrsoft.module.erpModel.caseErpSupplier.service.ICaseErpSupplierFormalService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xjrsoft.module.erpModel.caseErpSupplier.service.ICaseErpSupplierService;
import com.xjrsoft.module.erpModel.caseErpSupplier.vo.SupplierFormalDetailVo;
import com.xjrsoft.module.erpModel.caseErpSupplier.vo.SupplierTimeAxisVo;
import com.xjrsoft.module.organization.entity.User;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 供应商转正申请【case_erp_supplier_formal】 服务实现类
 * </p>
 *
 * @author hnyyzy
 * @since 2023-07-05
 */
@Service
@AllArgsConstructor
public class CaseErpSupplierFormalServiceImpl extends ServiceImpl<CaseErpSupplierFormalMapper, CaseErpSupplierFormal> implements ICaseErpSupplierFormalService {
    private final ICaseErpLogService caseErpLogService;

    private final ICaseErpSupplierService caseErpSupplierService;

    private final ObjectMapper objectMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean confirmation(AddCaseErpSupplierFormalDto dto) throws JsonProcessingException {
        // 目前转正逻辑，直接转成正式供应商
        CaseErpSupplier supplier = new CaseErpSupplier();
        supplier.setId(dto.getSupplierId());
        supplier.setState(1);
        supplier.setFormalState(4);
        caseErpSupplierService.updateById(supplier);
        //保存供应商转正表数据
        CaseErpSupplierFormal caseErpSupplierFormal = BeanUtil.toBean(dto, CaseErpSupplierFormal.class);
        boolean save = this.save(caseErpSupplierFormal);
        if (save) {
            // 添加时间轴数据
            User user = StpUtil.getTokenSession().get(GlobalConstant.LOGIN_USER_INFO_KEY, new User());
            CaseErpLog log = new CaseErpLog();
            log.setKeyId(dto.getSupplierId());
            log.setCategoryId(4);
            SupplierTimeAxisVo timeAxisVo = new SupplierTimeAxisVo();
            timeAxisVo.setTitle("成为正式供应商");
            timeAxisVo.setContent(new String[]{"最终审批人：" + user.getName()});
            timeAxisVo.setTime(LocalDateTime.now());
            log.setExecuteResultJson(objectMapper.writeValueAsString(timeAxisVo));
            caseErpLogService.save(log);
        }
        return true;
    }

    @Override
    public SupplierFormalDetailVo getConfirmationInfo(Long id) {
        CaseErpSupplierFormal supplierFormal = this.getOne(Wrappers.<CaseErpSupplierFormal>query().lambda()
                .eq(CaseErpSupplierFormal::getSupplierId, id).eq(CaseErpSupplierFormal::getEnabledMark, 1), false);
        SupplierFormalDetailVo formalDetailVo = BeanUtil.toBean(supplierFormal, SupplierFormalDetailVo.class);
        // 时间轴数据
        formalDetailVo.setTimeAxisVoList(this.getTimeAxisBySupplierId(id));
        return formalDetailVo;
    }

    /**
     * 获取时间轴数据
     *
     * @param id
     * @return
     */
    public List<Map<String, Object>> getTimeAxisBySupplierId(Long id) {
        List<CaseErpLog> logList = caseErpLogService.list(Wrappers.<CaseErpLog>query().lambda()
                .eq(CaseErpLog::getKeyId, id)
                .eq(CaseErpLog::getCategoryId, 4)
                .orderByDesc(CaseErpLog::getCreateDate));
        List<Map<String, Object>> resultList = logList.stream().map(log -> JSONObject.parseObject(log.getExecuteResultJson())).collect(Collectors.toList());
        return resultList;
    }


}
